<?php

class Shortener{
	const CODESET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~';
	const BASE    = 65;
	/**
	 * MAX $n value is 9.007.199.254.741.218 => shn-2FL7
	 * @param int $n
	 * @return string Shortened $n
	 */
	public static function shorten($n) {
		$shorten='';
		while($n>0){
			$shorten=substr(self::CODESET,($n%self::BASE),1).$shorten;
			$n=floor($n/self::BASE);
		}
		return $shorten;
	}
	/**
	 * @param string $converted
	 * @return int Recovered integer from the shortened value
	 */
	function unshorten($shorten) {
		 $c = 0;
		 for ($i=strlen($shorten); $i; $i--) {
		   $c+=strpos(self::CODESET,substr($shorten,(-1*($i-strlen($shorten))),1)) 
		         *pow(self::BASE,$i-1);
		 }
		 return $c;
	}
}


